<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">
          <template v-if="toggleSearchStatus">
            <a-col :md="4" :sm="8">
              <a-form-item label="停车场出口名称">
                <a-input placeholder="请输入停车场出口名称" v-model="queryParam.armName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="停车场出口编号">
                <a-input placeholder="请输入停车场出口编号" v-model="queryParam.armCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="ip地址">
                <a-input placeholder="请输入ip地址" v-model="queryParam.ipAddr"></a-input>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="4" :sm="8">

          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
    </div>
    <div>
      <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          @change="handleTableChange">

          <span slot="action" slot-scope="text, record">
           <a @click="handleCamera(record)">相机后台</a>
        </span>

      </a-table>
    </div>
    <park-arm-info-modal ref="modalForm" @ok="modalFormOk"></park-arm-info-modal>
    <select-arm-modal ref="modalSelectArm"></select-arm-modal>
  </a-card>
</template>

<script>
import ParkArmInfoModal from './modules/ParkArmInfoModal'
import SelectArmModal from './modules/SelectArmModal'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {deleteAction, httpAction, getAction, downFile} from '@/api/manage'
import {filterObj} from '@/utils/util';


export default {
  name: "ParkArmInfoList",
  mixins: [JeecgListMixin],
  components: {
    ParkArmInfoModal,
    SelectArmModal,
  },
  data() {
    return {
      timer: null,
      description: '车场出入口配置管理页面',
      columns: [
        {
          title: '停车场名称',
          align: "center",
          dataIndex: 'parkName'
        },
        {
          title: '停车场编码',
          align: "center",
          dataIndex: 'parkCode'
        },
        {
          title: '车道名称',
          align: "center",
          dataIndex: 'armName'
        },
        {
          title: 'ip地址',
          align: "center",
          dataIndex: 'ipAddr'
        },
        {
          title: '设备序列号',
          align: "center",
          dataIndex: 'serialNo'
        },
        {
          title: 'rtsp_url',
          align: "center",
          dataIndex: 'rtspUrl'
        },
        {
          title: 'video_url',
          align: "center",
          dataIndex: 'videoUrl'
        },
        {
          title: '出入口状态',
          align: "center",
          customRender: function (t, r, index) {
            if (r.armStatus == '1') {
              r.armStatus = '正常';
            } else if (r.armStatus == '2') {
              r.armStatus = '禁止';
            }
            return r.armStatus;
          }
        },
        {
          title: '出入口类型',
          align: "center",
          customRender: function (t, r, index) {
            if (r.armType == '0') {
              r.armType = '入口';
            } else if (r.armType == '1') {
              r.armType = '出口';
            }
            return r.armType;
          }
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {
            customRender: 'action'
          },
        },
      ],
      url: {
        list: "/parking/parkArmInfo/list",
        delete: "/parking/parkArmInfo/delete",
        deleteBatch: "/parking/parkArmInfo/deleteBatch",
        exportXlsUrl: "parking/parkArmInfo/exportXls",
        importExcelUrl: "parking/parkArmInfo/importExcel",
        edit: "/jddhlw/parkingQR/parkArmInfoQRcode",
      },
      queryParam: {
        "opTime_begin": "",
        "opTime_end": "",
        "type": "-1",
      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created() {
    this.keyupEnter()
  },
  destroyed: function () {
    window.clearInterval(this.timer)
  },
  methods: {

    handleCamera(record){
      window.open('http://' + record.ipAddr);

    },
    handlePerssion: function () {
      this.$refs.modalSelectArm.show();
    },
    handleExportXls(fileName) {
      let startTime = this.queryParam.opTime_begin;
      let endTime = this.queryParam.opTime_end;
      let dayTime = startTime;
      if (startTime === "") {
        this.$message.error("请选择导出开始日期");
        return
      }
      if (endTime === "") {
        this.$message.error("请选择导出结束日期");
        return
      }
      if (startTime !== "" && endTime !== "") {
        if (startTime !== endTime) {
          this.$message.error("仅支持1天数据导出,请选择导出日期");
          return
        }
      }
      if (startTime === "" && endTime !== "") {
        this.$message.error("请选择开始时间");
        return
      }
      if (startTime !== "" && endTime === "") {
        this.$message.error("请选择结束时间");
        return
      }
      this.queryParam.dayTime = dayTime;
      fileName = dayTime + fileName;
      let param = {...this.queryParam};
      if (this.queryParam.opTime_begin !== "" && this.queryParam.opTime_end !== "") {
        param.opTime_begin = this.queryParam.opTime_begin + " 00:00:00";
        param.opTime_end = this.queryParam.opTime_end + " 23:59:59";
      }
      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link);
          window.URL.revokeObjectURL(url);
        }
      })
    },
    onChangeStartTime(date, dateString) {
      this.queryParam.opTime_begin = dateString;
    },
    onChangeEndTime(date, dateString) {
      this.queryParam.opTime_end = dateString;
    },
    searchQuery() {
      let startTime = this.queryParam.opTime_begin;
      let endTime = this.queryParam.opTime_end;
      if (startTime !== "" && endTime !== "") {
        if (startTime > endTime) {
          this.$message.error("开始时间不能大于结束时间");
          return
        }
      }
      if (startTime === "" && endTime !== "") {
        this.$message.error("请选择开始时间");
        return
      }
      if (startTime !== "" && endTime === "") {
        this.$message.error("请选择结束时间");
        return
      }
      this.loadData(1);
    },
    getQueryParams() {
      var param = Object.assign({}, this.isorter, this.queryParam);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      param.parkCode = this.parkCode;
      if (this.queryParam.opTime_begin !== "" && this.queryParam.opTime_end !== "") {
        param.startTime = this.queryParam.opTime_begin + " 00:00:00";
        param.endTime = this.queryParam.opTime_end + " 23:59:59";
      }
      return filterObj(param);
    },
    async loadData(arg) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();
      if (this.parkCode === "") {
        params.parkCode = null;
      }
      this.loading = true;
      await getAction(this.url.list, params).then(async (res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false;
      })
    },
    batchDel: function () {
      if (!this.url.deleteBatch) {
        this.$message.error("请设置url.deleteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectionRows.length; a++) {
          ids += this.selectionRows[a].id + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            });
          }
        });
      }
    },
    handleDelete: function (invateuserdetailId) {
      if (!this.url.delete) {
        this.$message.error("请设置url.delete属性!")
        return
      }
      var that = this;
      deleteAction(that.url.delete, {invateuserdetailId: invateuserdetailId}).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          that.loadData();
        } else {
          that.$message.warning(res.message);
        }
      });
    },
    handleQr: function (record) {
      downFile(this.url.edit, record).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), 'qrcode.png')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          let type = '';

          if (record.armType === '入口') {
            type = '入口码'
          }
          if (record.armType === '出口') {
            type = '出口码'
          }
          link.setAttribute('download', record.parkName + type + ".png")
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          window.URL.revokeObjectURL(url)
        }
      }).finally(() => {
      })

    },
    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1);
        }
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
